﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace huaguwang.hgmanage.Power
{
    public partial class PowerGroup_ESon : PageBaseAdmin
    {
        private readonly D_T_Menu_Exp _Dal = new D_T_Menu_Exp();
        private readonly D_T_Button_Exp _DalBut = new D_T_Button_Exp();

        protected void Page_Load(object sender, EventArgs e)
        {
            WebUserToolBar1.EditInit(
              UserConst.Exp_GetUser,
              UserConst.Exp_GetButtonList,
              (i, but) =>
              {
                  switch(i)
                  {
                      case 0:
                          but.Attributes.Add("onclick", "return GetCheck();");
                          break;
                      case 1:
                          but.CausesValidation = false;
                          break;
                      default:
                          break;
                  }

              },
             UserConst.Exp_ToolBarErr
           );


            if(!IsPostBack)
            {
                //获取SKey,此段代码必须有
                ViewState.Add("SKey", Request.Params["SKey"].Exp_Trim());
                var id = Request.Params["ID"].Exp_IntTryParse();
                ViewState.Add("ID", id);

                if(id > 0)
                {
                    SetData();
                }
                else
                {
                    var sKey = this.WebUserToolBar1.EditSKey;
                    this.Exp_PageAlterHref("找不到相关资料", "powergroup.aspx?" + sKey);
                }
            }

        }
        private void SetData()
        {
            var model = _Dal.GetListAllP();//绑定父菜单
            this.Repeater1.DataSource = model;
            this.Repeater1.DataBind();
        }
        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button0(object sender, EventArgs e)
        {
            var s = Request.Params["Hidden1"];
            var id = ViewState["ID"].Exp_IntTryParse();
            var items = s.Exp_SplitToList<int>(',', Sel => Sel.Exp_IntTryParse()).Where(Sel => Sel > 0);

            if(items == null || items.Count() <= 0 || id <= 0)
            {
                Page.Exp_PageAlter("没有找到可以操作的资料");
            }
            else
            {
                var intRef = _DalBut.UpdatePower(items.ToList(), id);
                if(intRef > 0)
                {
                    this.Exp_PageWriteLog(Log_Type.update, "权限详细信息", intRef);
                    var sKey = this.WebUserToolBar1.EditSKey;
                    this.Exp_PageAlterHref("操作成功", "powergroup.aspx?" + sKey);
                }
                else
                {

                }
            }

            ///*
            //    公共的差错校验
            //*/



            //var iRef = 0;
            //if (0 >= model.id)
            //{
            //    iRef = _Dal.AddExp(model);
            //}
            //else
            //{
            //    /*
            //        修改的专用判断
            //     */


            //    var Temp = _Dal.ExistsExp(model.id);
            //    if (!Temp)
            //    {
            //        this.Exp_PageAlter("资料不存在,可能被其他人删除");
            //        return;

            //    }

            //    iRef = _Dal.UpdateExp(model);
            //}

            //if (iRef > 0)
            //{
            //    this.Exp_PageWriteLog("", "操作成功:" + iRef, "菜单资料维护", true);
            //    var sKey = Server.UrlEncode(ViewState["SKey"].Exp_Trim());
            //    this.Exp_PageAlterURL("操作成功", "PowerGroup.aspx?SKey=" + sKey);

            //}
            //else
            //{
            //    this.Exp_PageAlter("操作失败");
            //}

        }

        protected void WebUserToolBar1_Button1(object sender, EventArgs e)
        {
            var sKey = this.WebUserToolBar1.EditSKey;
            Response.Redirect("powergroup.aspx?" + sKey);
        }

        protected void Repeater1_DataBinding(object sender, EventArgs e)
        {
        }

        //
        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            var RP = (e.Item.FindControl("Repeater2") as Repeater);

            var model = (e.Item.DataItem as M_T_Menu).id;

            var items = _Dal.GetListSon(model);

            RP.DataSource = items;
            RP.DataBind();

        }

        protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {

        }

        protected void Repeater2_ItemDataBound1(object sender, RepeaterItemEventArgs e)
        {
            var cl = (e.Item.FindControl("Repeater3") as Repeater);
            int id = ViewState["ID"].Exp_IntTryParse();
            var model = (e.Item.DataItem as M_T_Menu).id;
            var items = _DalBut.GetListBut(model, id);

            cl.DataSource = items;
            cl.DataBind();
        }
    }
}
